package desafio;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;


public class Carta extends JButton implements MouseListener{
	private int numero;
	private char naipe;
	ImageIcon imgVerso = null;
	ImageIcon img = null;
	ImageIcon imgNaipe = null;

	public Carta(int numero, char naipe) {
		try{
			if(numero == 0 || numero == 52){
				img = new ImageIcon(ClassLoader.getSystemResource("img/" + numero + naipe + ".png"));
			}else{
				imgNaipe = new ImageIcon(ClassLoader.getSystemResource("img/" + naipe + ".png"));
			}
			imgVerso = new ImageIcon(ClassLoader.getSystemResource("img/verso.png"));
		}catch(Exception e){
			e.printStackTrace();
		}
		if(naipe == 'd'){
			this.setIcon(img);
			if(numero == 0){
				this.setPreferredSize(new Dimension(100,150));
			}else{
				this.setPreferredSize(new Dimension(137,167));
			}
		}else{
			this.setPreferredSize(new Dimension(100,150));
		}
		this.setMargin(new Insets(0, 0, 0, 0));		
		this.setBackground(null);
		this.setBorder(null);
		this.setDisabledIcon(imgVerso);				

		this.numero = numero;
		this.naipe = naipe;
		this.addMouseListener(this);
	}


	public int getNumero() {
		return numero;
	}

	public char getNaipe() {
		return naipe;
	}

	public String toString() {
		return numero + ":" + naipe;		
	}

	public void paint(Graphics g) {
		//super.paint(g);
		try{				
			Graphics2D g2 = (Graphics2D) g;

			if(isEnabled() && naipe != 'd'){			
				g2.setColor(Color.WHITE);		
				g2.fillRoundRect(3, 3, 93, 143, 20, 20);

				if(naipe == 'c' || naipe == 'o'){
					g2.setColor(Color.RED);
				}else{
					g2.setColor(Color.BLACK);
				}

				g2.drawRoundRect(3, 3, 93, 143, 20, 20);
				g2.drawRoundRect(4, 4, 91, 141, 17, 17);
				g2.drawRoundRect(5, 5, 89, 139, 14, 14);				

				g2.setFont(new Font("Arial",Font.BOLD,18));
				g2.drawString(getStringNumero(), 10, 22);
				g2.drawImage(imgNaipe.getImage(), 35, 60, null);		
				g2.drawString(getStringNumero(), (numero == 10)?70:80, 140);
				if(getBorder()!=null){
					g2.setColor(Color.WHITE);
					g2.drawRoundRect(0, 0, 98, 148, 20, 20);
					g2.drawRoundRect(1, 1, 96, 146, 17, 17);
					g2.drawRoundRect(2, 2, 94, 144, 14, 14);
				}
			}else{
				switch (numero) {
				case 0:
					g2.drawImage(img.getImage(), 0, 3, null);
					break;
				case 52:
					g2.drawImage(img.getImage(), 0, 3, null);
					break;				
				default:
					g2.drawImage(imgVerso.getImage(), 0, 3, null);
					break;
				}
			}
		}catch (Exception e) {
			System.out.println("Erro ao desenhar cartas");
		}
	}
	public String getStringNumero(){
		String string = String.valueOf(this.numero); 
		switch(this.numero){
		case 1:
			string = "A";
			break;
		case 11:
			string = "J";
			break;
		case 12:
			string = "Q";
			break;
		case 13:
			string = "K";
			break;
			
		}
		return string;
	}

	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {
		this.setCursor(new Cursor(Cursor.HAND_CURSOR));
	}
	public void mouseExited(MouseEvent e) {
		this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
	}
	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
}
